package com.eatme.web.app.coupon.distribution.entity;

import com.eatme.web.app.coupon.common.vo.CouponTemplateSDK;
import com.eatme.web.app.coupon.distribution.constant.CouponStatus;
import com.eatme.web.app.coupon.distribution.converter.CouponStatusConverter;
import com.eatme.web.app.coupon.distribution.serialization.CouponSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

/**
 * 优惠券(用户领取优惠券记录)实体表
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name="coupon")
@JsonSerialize(using = CouponSerialize.class)
public class Coupon {


    /** 自增主键 */
    @Id
    @GeneratedValue(strategy =GenerationType.IDENTITY)
    @Column(name="id",nullable = false)
    private Integer id;

    /** 关联优惠券模板的主键(逻辑外键) */
    @Column(name="template_id",nullable = false)
    private Integer templateId;

    /** 领取用户 */
    @Column(name="user_id",nullable = false)
    private Long userId;

    /** 优惠券码 */
    @Column(name="coupon_code",nullable = false)
    private String couponCode;

    /** 领取时间 */
    @Column(name="assign_time",nullable = false)
    @CreatedDate
    private Date assignTime;

    /** 领取时间 */
    // @Basic
    @Column(name="status",nullable = false)
    @Convert(converter= CouponStatusConverter.class)
    private CouponStatus status;

    /** 用户优惠券对应的模板信息 */
    @Transient
    private CouponTemplateSDK templateSDK;

    /**  */
    public Coupon() {

    }

    public Coupon(Integer id, Integer templateId, Long userId, String couponCode, Date assignTime, CouponStatus status, CouponTemplateSDK templateSDK) {
        this.id = id;
        this.templateId = templateId;
        this.userId = userId;
        this.couponCode = couponCode;
        this.assignTime = assignTime;
        this.status = status;
        this.templateSDK = templateSDK;
    }

    /**
     * 返回一个无效的 Coupon 对象
     * @return
     */
    public static Coupon invalidCoupon() {
        Coupon coupon =new Coupon();
        coupon.setId(-1);
        return coupon;
    }

    /**  */
    public Coupon(Integer templateId, Long userId, String couponCode, CouponStatus status) {
        this.templateId = templateId;
        this.userId = userId;
        this.couponCode = couponCode;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getTemplateId() {
        return templateId;
    }

    public void setTemplateId(Integer templateId) {
        this.templateId = templateId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getCouponCode() {
        return couponCode;
    }

    public void setCouponCode(String couponCode) {
        this.couponCode = couponCode;
    }

    public Date getAssignTime() {
        return assignTime;
    }

    public void setAssignTime(Date assignTime) {
        this.assignTime = assignTime;
    }

    public CouponStatus getStatus() {
        return status;
    }

    public void setStatus(CouponStatus status) {
        this.status = status;
    }

    public CouponTemplateSDK getTemplateSDK() {
        return templateSDK;
    }

    public void setTemplateSDK(CouponTemplateSDK templateSDK) {
        this.templateSDK = templateSDK;
    }
}
